package com.study_platform.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study_platform.entity.ProblemSolvingRecord;
import com.study_platform.entity.Question;
import com.study_platform.service.ProblemSolvingRecordService;
import com.study_platform.service.QuestionService;
import com.study_platform.util.VO.QuestionRecord;
import com.study_platform.util.VO.RecordInfo;
import com.study_platform.util.VO.TestInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/study_platform/problem-solving-record")
public class ProblemSolvingRecordController {
    @Resource
    private ProblemSolvingRecordService problemSolvingRecordService;
    @Resource
    private QuestionService questionService;

    @GetMapping("/getRecordPage/")
    public JSONObject getPage(@RequestParam Integer userId){
        JSONObject jsonObject = new JSONObject();
        List<ProblemSolvingRecord> problemSolvingRecords = problemSolvingRecordService.lambdaQuery()
                .eq(ProblemSolvingRecord::getUserId, userId)
                .orderByDesc(ProblemSolvingRecord::getTestId)
                .list();
        List<Integer> testIds = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        for (ProblemSolvingRecord record : problemSolvingRecords) {
            if (!set.contains(record.getTestId())){
                testIds.add(record.getTestId());
                set.add(record.getTestId());
            }
            if (set.size() >= 6){
                break;
            }
        }
        jsonObject.put("infos", testIds);
        return jsonObject;
    }

    @GetMapping("/getRecord/")//做题记录
    public List<ProblemSolvingRecord> getRecord(@RequestParam Integer userId) {
        List<ProblemSolvingRecord> problemSolvingRecordList = problemSolvingRecordService.list();
        problemSolvingRecordList.removeIf(problemSolvingRecord -> !problemSolvingRecord.getUserId().equals(userId));
        return problemSolvingRecordList;
    }

    @GetMapping("/getTest/{user_id}/")
    public JSONObject getTest(@PathVariable Integer user_id){
        List<ProblemSolvingRecord> list = problemSolvingRecordService.lambdaQuery()
                .eq(ProblemSolvingRecord::getUserId, user_id)
                .orderByDesc(ProblemSolvingRecord::getTime)
                .groupBy(ProblemSolvingRecord::getTestId)
                .list();

        Set<Integer> set = new HashSet<>();
        List<TestInfo> testInfos = new ArrayList<>();
        for (ProblemSolvingRecord record : list) {
            if (!set.contains(record.getTestId())){
                set.add(record.getTestId());
                testInfos.add(new TestInfo(record.getTestId(), record.getTime()));
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("testInfos", testInfos);
        return jsonObject;
    }

    @GetMapping("/searchRecord/{testId}/{userId}/")
    public JSONObject searchRecord(@PathVariable Integer testId,
                                   @PathVariable Integer userId){
        List<ProblemSolvingRecord> records = problemSolvingRecordService.lambdaQuery()
                .eq(ProblemSolvingRecord::getUserId, userId)
                .eq(ProblemSolvingRecord::getTestId, testId)
                .list();

        List<QuestionRecord> questionRecords = new ArrayList<>();
        for (ProblemSolvingRecord record : records){
            QuestionRecord questionRecord = new QuestionRecord();
            Question question = questionService.getById(record.getQuestionId());
            questionRecord.setContent(question.getContent());
            questionRecord.setSelection_a(question.getSelectionA());
            questionRecord.setSelection_b(question.getSelectionB());
            questionRecord.setSelection_c(question.getSelectionC());
            questionRecord.setSelection_d(question.getSelectionD());
            questionRecord.setAnswer(question.getAnswer());
            questionRecord.setMyAnswer(record.getAnswer());
            questionRecords.add(questionRecord);
        }
        Set<QuestionRecord> set = new HashSet<>(questionRecords);

        List<QuestionRecord> ans = new ArrayList<>(set);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("records", ans);
        return jsonObject;
    }

    @GetMapping("/getScore/{testId}/{userId}/")
    public int getScore(@PathVariable Integer testId,
                        @PathVariable Integer userId) {
        int right = 0;
        List<ProblemSolvingRecord> problemSolvingRecordList = problemSolvingRecordService.lambdaQuery()
                .eq(ProblemSolvingRecord::getUserId, userId)
                .eq(ProblemSolvingRecord::getTestId, testId)
                .list();
        for (ProblemSolvingRecord problemSolvingRecord : problemSolvingRecordList) {
            String answer = problemSolvingRecord.getAnswer();
            if (answer.equalsIgnoreCase(questionService.
                    getById(problemSolvingRecord
                            .getQuestionId())
                    .getAnswer())) {
                right++;
            }
        }
        int ans;
        if (right > 0){
            ans = (int) ((100.0 * right) / problemSolvingRecordList.size());
        }else{
            ans = 0;
        }
        return ans;
    }

    @PutMapping("/add/")
    public JSONObject add(@RequestBody ProblemSolvingRecord problemSolvingRecord){
        JSONObject jsonObject = new JSONObject();
        if (problemSolvingRecordService.saveOrUpdate(problemSolvingRecord)){
            jsonObject.put("error_message", "success");
        }else{
            jsonObject.put("error_message", "success");
        }
        Question question = questionService.getById(problemSolvingRecord.getQuestionId());
        if (question.getAnswer().equalsIgnoreCase(problemSolvingRecord.getAnswer())){
            jsonObject.put("result", "dui4");//对
        }else{
            jsonObject.put("result", "cuo4");//错
        }
        return jsonObject;
    }
    @DeleteMapping("/delete/{id}/")
    public JSONObject delete(@PathVariable Integer id){
        JSONObject jsonObject = new JSONObject();
        if (problemSolvingRecordService.removeById(id)){
            jsonObject.put("error_message", "success");
        }else{
            jsonObject.put("error_message", "success");
        }
        return jsonObject;
    }
}

